<?php
namespace app\common\model;

use think\Log;
use think\Model;

class AuthRule extends Model
{
    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['type']) && is_numeric($params['type'])) {
            $where['type'] = $params['type'];
        }

        if (isset($params['status']) &&  is_numeric($params['status'])) {
            $where['status'] = $params['status'];
        }

        if (isset($params['pid']) &&  is_numeric($params['pid'])) {
            $where['pid'] = $params['pid'];
        }

        if (isset($params['son_rule']) &&  $params['son_rule']) {
            $where['pid'] = ['gt', 0];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'sort desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);

        return $data->toArray();
    }

    /**
     * 查询列表,手动分页，防止框架 paginate 对于page会形成全局作用范围的问题
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getListByPage(array $params, array $limit = [], $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['username']) && !empty($params['username'])) {
            $where['username'] = $params['username'];
        }

        if (isset($params['username_like']) && !empty($params['username_like'])) {
            $where['username'] = ['like', '%'.$params['username_like'].'%'];
        }

        if (isset($params['status']) && is_numeric($params['status'])) {
            $where['status'] = $params['status'];
        }

        if (isset($params['group_status']) &&  is_numeric($params['group_status'])) {
            $where['group_status'] = $params['group_status'];
        }

        if (isset($params['code']) &&  !empty($params['code'])) {
            $where['code'] = $params['code'];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        if (empty($limit)) {
            $limit = build_limit(1, 20);
        }

        $data = $this->where($where)->field($fields)->order($order)->limit($limit['begin'], $limit['offset'])->select();
        foreach ($data as &$v) {
            $v = $v->toArray();
        }

        $return = [
            'total' => $count,
            'data'  => $data,
        ];
        return $return;
    }

    /**
     * @param $id
     * @param array $req
     * @return bool | integer
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $data = $req;

        $res = $this->allowField(true)->save($data, ['id' => $id]);
        if ($res === false || $res > 1) {
            Log::record('更新资产数据失败：' . $this->getLastSql());
            return false;
        }

        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $data
     * @return bool|mixed
     */
    public function addData(array $data)
    {
        $res = $this->allowField(true)->data($data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $return;
    }

    /**
     * 检查是否存在重复的菜单
     * @param $title
     * @param $need_info
     * @return bool
     */
    public function checkTitleExists($title, $need_info = false)
    {
        $where = [
            'title' => $title,
        ];
        $info = $this->where($where)->field('id,title')->find();
        if (!empty($info)) {
            if ($need_info) {
                return $info->toArray();
            }
            return true;
        }

        return false;
    }

    /**
     * 检查是否存在重复的菜单
     * @param $name
     * @param $need_info
     * @return bool
     */
    public function checkNameExists($name, $need_info = false)
    {
        $where = [
            'name' => $name,
        ];
        $info = $this->where($where)->field('id,name')->find();
        if (!empty($info)) {
            if ($need_info) {
                return $info->toArray();
            }
            return true;
        }

        return false;
    }

    /**
     * 获取所有的规则（菜单）并整理成树形结构
     */
    public function getAllRuleList()
    {

    }
}